<template>
  <common-edit-dialog
    :title="title"
    :visible.sync="open"
    @cancel="cancel"
    @submit="submitForm"
    :buttonLoading="buttonLoading"
  >
    <el-form
      :model="form"
      label-width="130px"
      ref="form"
      :rules="rules"
      @submit.native.prevent
    >
      <el-row :gutter="18">
        <el-col :span="12">
          <el-form-item label="系统编码" prop="businessCode">
            <common-input
              v-model="form.businessCode"
              :disabled="isEdit"
              :maxlength="32"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="系统名称" prop="businessName">
            <common-input
              v-model="form.businessName"
              :maxlength="50"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="等保定级" prop="protectLevel">
            <el-select v-model="form.protectLevel" filterable clearable>
              <el-option
                v-for="item in protectLevelList"
                :key="item.label"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用途" prop="businessUse">
            <common-input v-model="form.businessUse" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人" prop="userCode">
            <choose-dialog
              :selectValue="form.userCode"
              @submitDialog="submitDialog"
              @clearInfo="clearInfo"
            ></choose-dialog>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="关键信息基础设施" prop="isFacilities">
            <el-radio-group v-model="form.isFacilities">
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="简介" prop="introduce">
            <common-input
              v-model="form.introduce"
              type="textarea"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24">
          <el-form-item label="内容" prop="remark">
            <common-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
import { edit, getInfo, add } from "@/api/system/businessSystem/businessSystem";
import ChooseDialog from "./chooseDialog.vue";
export default {
  name: "add",
  components: {
    ChooseDialog,
  },
  props: {
    isEmbed: {
      type: Boolean,
      default: () => false,
    }, //是否被其他页面引用
  },
  data() {
    return {
      title: "",
      isEdit: false,
      open: false,
      buttonLoading: false,
      form: {
        businessCode: "",
        businessName: "",
        userCode: "",
        businessUse: "",
        remark: "",
        isDefault: "N",
        protectLevel: null,
        isFacilities: 1,
        introduce: null,
      },
      protectLevelList: [
        {
          label: "未通过网络安全等级保护测评",
          value: 0,
        },
        { label: "网络安全等级保护4级", value: 4 },
        { label: "网络安全等级保护3级", value: 3 },
        { label: "网络安全等级保护2级", value: 2 },
        { label: "网络安全等级保护1级", value: 1 },
      ],
      rules: {
        businessCode: [
          { required: true, message: "请输入系统编码", trigger: "blur" },
        ],
        businessName: [
          { required: true, message: "请输入系统名称", trigger: "blur" },
        ],
        protectLevel: [
          { required: true, message: "请选择等保定级", trigger: "change" },
        ],
        isFacilities: [
          {
            required: true,
            message: "请选择关键信息基础设施",
            trigger: "change",
          },
        ],
        introduce: [{ required: true, message: "请输入简介", trigger: "blur" }],
      },
      queryLevelList: [
        {
          label: "一级",
          value: 1,
        },
        {
          label: "二级",
          value: 2,
        },
        {
          label: "三级",
          value: 3,
        },
      ],
    };
  },
  methods: {
    cancel() {
      this.resetForm();
      this.open = false;
    },
    resetForm() {
      this.form = {
        businessCode: "",
        businessName: "",
        userCode: "",
        businessUse: "",
        remark: "",
        isDefault: "N",
        protectLevel: null,
        isFacilities: 1,
        introduce: null,
      };
    },
    showDialog(id) {
      if (id) {
        this.open = true;
        getInfo({ id }).then((res) => {
          if (res && res.code === 200) {
            this.form = res.data;
          } else {
            this.$GlobalErrorConfirm("数据加载失败", res.msg);
          }
        });
      } else {
        this.open = true;
      }
      this.isEdit = !!id;
      this.title = this.isEdit ? "编辑业务系统" : "新增业务系统";
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id) {
            edit(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.cancel();
                  this.$emit("refresh");
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg);
                }
              })
              .finally(() => (this.buttonLoading = false));
          } else {
            if (!this.isEmbed) {
              add(this.form)
                .then((res) => {
                  if (res && res.code === 200) {
                    this.$GlobalSuccessMessage("新增成功");
                    this.cancel();
                    this.$emit("refresh");
                  } else {
                    this.$GlobalErrorConfirm("新增失败", res.msg);
                  }
                })
                .finally(() => (this.buttonLoading = false));
            } else {
              this.$emit("save", this.form);
            }
          }
        }
      });
    },
    submitDialog(info) {
      this.form.userCode = info.userName;
    },
    clearInfo() {
      this.form.userCode = "";
    },
  },
};
</script>

<style scoped></style>
